package com.erlang.spring.service.impl;

import com.erlang.spring.dao.AccountDao;
import com.erlang.spring.pojo.Account;
import com.erlang.spring.service.DealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description：交易接口实现
 */
@Service("dealService")
public class DealServiceImpl implements DealService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public Integer doDeal(Float money, Integer payAccountId, Integer receiptAccountId) {
        int flag = 0;
        flag = accountDao.dealMoney(payAccountId, -money);
        if (flag==0){
            throw new RuntimeException("交易不成功");
        }
        int a = 1/0;
        flag= flag+accountDao.dealMoney(receiptAccountId, money);
        if (flag==1){
            throw new RuntimeException("交易不成功");
        }
        return flag;
    }

    @Override
    public void printAccountInfo(Integer payId, Integer rePayId) {
        Account payAccount = accountDao.findAccountById(payId);
        System.out.println("支付者账号余额："+payAccount.getMoney());
        Account receiptAccount = accountDao.findAccountById(rePayId);
        System.out.println("收款者账户余额："+receiptAccount.getMoney());
        Float sumMoney = payAccount.getMoney()+receiptAccount.getMoney();
        System.out.println("两人账户总余额："+sumMoney);
    }


}